var currentSegment="MSW_12000";
var rClickImg = 3;
var kxPos,kyPos;
var hsArea=false;
var qCtrl,segID;
var textFieldValue;
var slidFlag=false;

function loadQuestions(qctrl, sID){
	qCtrl = qctrl;
	segID = sID;		
	drawHotspots();
	slide(qCtrl);
}

function drawHotspots(){	
	console.log("in draw....")
	$('#questioncontainer').html('');
	
	var curItem, newItem;	
	for(var i=0;i<questionHotspots.length;i++){
		if(questionHotspots[i].id==segID){
			for(z in questionHotspots[i].htmls) {
				curItem = questionHotspots[i].htmls[z];
				newItem = $(curItem[4]);
				newItem.css({
								position:"absolute",
								border: "0px",
								"z-index": 9,
								left: curItem[0]+"px",
								top: curItem[1]+"px",
								width: (curItem[2] - curItem[0])+"px",
								height: (curItem[3] - curItem[1])+"px"
				});
				$('#questioncontainer').append(newItem);
			}
			for(j in questionHotspots[i].marks) {
				curItem = questionHotspots[i].marks[j];
				newItem = $("<div></div>");
				newItem.attr("id", j);
				
				newItem.attr("onclick", "doClickHandlerInQues(event, this);correctClick=true;");
				newItem.attr("onmouseover", "doOverActionInQues(event, this)");
				newItem.attr("onmouseout", "doOutActionInQues(event, this)");
				newItem.attr("onmouseup", "doClickHandlerInQues(event, this);correctClick=true;");
				newItem.bind("contextmenu",function(e){
					console.log("rrrrrrrrrrrrrrrrrrr")
					actionOffice(e, this);
					return false;			
				});
				
				newItem.css({"position":"absolute","display":"block","z-index":"2000","left":curItem[0]+"px","top":curItem[1]+"px","width":(curItem[2]-curItem[0])+"px",						"height": (curItem[3] - curItem[1])+"px"});
				if(j.substring(0,1)=="s"){hsArea=true;newItem.css({"background-color":"red","opacity":"0.2"})}
				$('#questioncontainer').append(newItem);
			}
			for(var k in questionHotspots[i].action) {
				actionType = k.substring(0,1);
				
				if ('hB'.indexOf(actionType)>=0) {
					curItem = questionHotspots[i].action[k];
					newItem = $("<div></div>");
					newItem.attr("id", k);
					newItem.css({
									display: "none",
									"background-color":(actionType=='h'?"LightSteelBlue":""),
									border:(actionType=='b'?"1px solid lightblue":(actionType=='B'?"4px solid LightSteelBlue":"0px")),
									'box-sizing': 'border-box',
									opacity: 0.5,
									position:"absolute",
									left: curItem[0]+"px",
									top: curItem[1]+"px",
									width: (curItem[2] - curItem[0])+"px",
									height: (curItem[3] - curItem[1])+"px"
					});					
					$('#questioncontainer').append(newItem);
				}
			};
			screenPage=i;
			break;
		}	
	}
}

function slide(qCtrl){
	$("#m_More").click(function(e){
		if(e.target.id=="m_More"){
			isShowDropdown = true;
			$("#glasscontainer").html('');
		}
	});
	
	$("#maincontainer").click(function(e){
		if(e.target.id=="n_Styles"){
			currentSegment="MSW_05002";
			isShowRightClickMenu=false;
			isShowDropdown=true;
			loadSegments(questionData[2].path, questionData[2].size, questionData[2].id);
			$("#rightclickmenucontainer").html('');
			$("#rightclickmenu").remove();
		}else if(e.target.id=="m_Normal" && slidFlag){
			incorrectMsg=true;
			loadSegments(questionData[6].path, questionData[6].size, questionData[6].id);
		}else if(e.target.id!="m_Row1of6"){
			$("#sliding").remove();
		}
	});
	
	$("#m_Row1of6").click(function(e){
		if(e.target.id=="m_Row1of6" && !slidFlag){
			incorrectMsg=true;slidFlag=true;
			$("#maincontainer").append('<img id="sliding" src="../images/item05/slide.png" style="position:absolute;left:565px;top:55px;">')
		}else if(slidFlag ){
			incorrectMsg=false;
		}
	});
	
	if(currentSegment=="MSW_05010")$("input[id=t_FilenameTxt]").select();
}

function doRightClickUsingKeys(ctrl){
	console.log("doRightClickUsingKeys..."+currentSegment)
	if(currentSegment == "MSW_12000"){
		kxPos=265;kyPos=250;
		doRightClickHandler(ctrl)
	}
}

function doClickHandlerInQues(event, ctrl){
	if(!isShowDropdown){
		hsArea=false;
	}
	actionOffice(event, ctrl)			
}

$(document).bind("dblclick",function(e) {
	
});

$(document).bind("click",function(e) {
	
	if(e.target.id=="t_ApplyBtnTxt"){
		if(textFieldValue != undefined)
		if(textFieldValue.toLowerCase() =="heading 2"){
			incorrectMsg=true;
			correctClick=true;
			loadSegments(questionData[6].path, questionData[6].size, questionData[6].id);
			
		}
		
	}
});
		
$(document).keyup(function(e){
	console.log("in doc key up"+currentSegment)
	if(currentSegment=="MSW_05020"){
		$("#glasscontainer").html('');
	}
});

function doOverActionInQues(event, ctrl) {	
	
	if(ctrl.id=="m_heading_1_style" || 
	ctrl.id=="m_heading_2_style" || 
	ctrl.id=="m_heading_3_style" || 
	ctrl.id=="m_no_spacing_style" || 
	ctrl.id=="m_title_style" || 
	ctrl.id=="m_subtitle_style" || 
	ctrl.id=="m_subtle_emphasis_style" || 
	ctrl.id=="m_emphasis_style" || 
	ctrl.id=="m_intense_emphasis_style" || 
	ctrl.id=="m_strong_style" || 
	ctrl.id=="m_quote_style" || 
	ctrl.id=="m_intense_quote_style" || 
	ctrl.id=="m_subtle_reference_style" || 
	ctrl.id=="m_intense_reference_style" || 
	ctrl.id=="m_list_paragraph_style" || 
	ctrl.id=="m_book_title_style"){		
		var preview = "../images/item05/"+ctrl.id.substring(2)+"_preview.png";		
		$("#maincontainer").append('<img id="previewshow" style="position:absolute;display:block;z-index:96;left:155px;top:336px;">')
		$('#previewshow').attr('src',preview);		
	}else{$('#previewshow').remove();}
	doAllActionInQues(event, ctrl, 5);
	clearKeyHighlightsInDropdown();
	//console.log("over in ques.."+ctrl.id)
	if((ctrl.id=="m_NoSpacing"||
		ctrl.id=="m_Heading1"||
		ctrl.id=="m_Heading2"||
		ctrl.id=="m_Heading3"||
		ctrl.id=="m_Heading4"||
		ctrl.id=="m_Title"||
		ctrl.id=="m_Subtitle"||
		ctrl.id=="m_SubtitleEmphasis"||
		ctrl.id=="m_Emphasis"||
		ctrl.id=="m_IntenseEmphasis"||
		ctrl.id=="m_Strong"||
		ctrl.id=="m_Quote"||
		ctrl.id=="m_IntenseQuote"||
		ctrl.id=="m_SubtitleRef"||
		ctrl.id=="m_IntenseRef"||
		ctrl.id=="m_BookTitle") && currentSegment=="MSW_05020"){
		//console.log("in sub="+event.pageY+""+questionHotspots[5].marks[ctrl.id][3])
		$('#arrow').remove();
		var yVal=questionHotspots[5].marks[ctrl.id][3]+122;//event.pageY;
		$('#maincontainer').append('<img id="arrow" src="../images/item05/arrow.png" style="position:absolute;display:block;z-index:97;left:515px;">')
		$('#arrow').css({"top":yVal});
		//$('#arrow').css({"position":"absolute","border-left":"5px solid transparent","border-right":"5px solid transparent","border-top":"5px solid black","z-index":"9999","display":"block","width":"0px","height":"0px","left":"event.pageX","opacity":"1"})
	}else{$('#arrow').remove();}
}

function doOutActionInQues(event, ctrl) {
	doAllActionInQues(event, ctrl, 5, true);
}

function doAllActionInQues(event, ctrl, idx, isActionRevert) {
	//console.log("actionList in ques="+ctrl.id+""+idx)
	var actionList = questionHotspots[screenPage].marks[ctrl.id][idx];	
	
	for(var i=0;i<actionList.length;i++) {
		doAction(event, actionList[i], isActionRevert);
	}
}

function tfKeyUpHandler(){
	isTextField=true;
	textFieldValue = $("input[id=t_FilenameTxt]").val();
	$("input[id=t_ApplyBtnTxt]").attr("value","New");
	if(event.which==13 && textFieldValue.toLowerCase()=="heading 2"){
		//currentSegment="MSW_05020";
		loadSegments(questionData[6].path, questionData[6].size, questionData[6].id)
	}if(event.which==13){
		isTextField=false;
	}if(textFieldValue.toLowerCase()=="heading 2"){
		$("input[id=t_ApplyBtnTxt]").attr("value","Apply");
	}
}

function onTextFieldClickHandler(){
	incorrectMsg=true;
}


var liItem,arrKeyCount=0;

function doDropdownkeyHandler(keycode){
	console.log(isShowDropdown+"doDropdownkeyHandler..."+canTakeKeyDown)
	//incorrectMsg=false;
	var curItem,liItem=[];
	
	for(var i in questionHotspots[screenPage].marks) {
		curItem = i.substring(2)		
		liItem.push(curItem)		
	}
	
	if(keycode==40){//downArrow
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0"});	
		arrKeyCount++;
	}else if(keycode==38){//upArrow		
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0"});		
		arrKeyCount--;
		if(arrKeyCount<0){arrKeyCount=liItem.length;}
	}
	if(liItem.length>arrKeyCount){		
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0.5"});	
	}else{
		if(keycode==40){
			arrKeyCount=0;
		}else if(keycode==38){
			arrKeyCount=liItem.length-1;
		}
		$("#m_"+liItem[arrKeyCount]).css({"background-color":"LightSteelBlue","opacity":"0.5"});
	}
	if(currentSegment=="MSW_05011")
	$("input[id=t_StyleNameTxt]").attr("value",liItem[arrKeyCount]);
	console.log("in dropdown handler"+keycode, event.ctrlKey, event.shiftKey, event.altKey, true, liItem[arrKeyCount])
	rightClickKeyHandler(keycode, event.ctrlKey, event.shiftKey, event.altKey, true, liItem[arrKeyCount])
}


function clearKeyHighlightsInDropdown(){
	for(var i in questionHotspots[screenPage].marks) {
		curItem = i.substring(2)		
		$("#m_"+curItem).css({"background-color":"LightSteelBlue","opacity":"0"});
	}
}